import * as types from './mutation-types'

export default {
    // 改边侧边栏的展示状态
    [types.CHANGE_SIDEBAR_STATU](state, falg) {
        state.sidebarStatu = falg
    },
    // 修改当前展示主题的id
    [types.CHANGE_CURRENT_THEME_ID](state, id) {
        state.currentThemeId = id
    },
    // 增加接口加载过来的新闻 ID
    [types.ADD_STORIES_ID](state, storyObj) {
        if (storyObj.type == 'init') {
            state.storiesId = storyObj.ids
        } else {
            state.storiesId = state.storiesId.concat(storyObj.ids)
        }
    },
    // 增加或删除收藏、
    [types.CHANGE_COLLECT_STORIES](state, story) {
        let id = story.id;
        let flag = false;
        let index;
        if (state.collectStories.length === 0) {
            state.collectStories.push(story);
            state.isCollect = !state.isCollect;
            return;
        }
        state.collectStories.map((item, index) => {
                if (item.id === state.id) {
                    flag = true;
                    index = index;
                }
            })
            // flag = true 则已经收藏
        if (!flag) {
            state.collectStories.push(story);
        } else { // 已收藏则删除
            state.collectStories.splice(index, 1);
        }
        state.isCollect = !state.isCollect;
    },
    // 根据ID获取收藏状态
    [types.CHANGE_COLLECT_STATUS](state, id) {
        let collectIds = [];
        if (state.collectStories.length > 0) {
            state.collectStories.forEach(item => {
                collectIds.push(item.id);
            });
            if (collectIds.indexOf(id) < 0) {
                state.isCollect = false;
            } else {
                state.isCollect = true;
            }
        }
    }
}